package com.itheima.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 自定义元数据对象处理器
 */
@Component
@Slf4j
public class MyMetaObjecthandler_自动填充 implements MetaObjectHandler {
    /**
     * 插入操作，自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("公共字段自动填充[insert]...");
        log.info(metaObject.toString());


        //这种设置按照有的字段进行使用，如果没有则不用
        this.fillStrategy(metaObject,"createTime", LocalDateTime.now());
        this.fillStrategy(metaObject,"updateTime",LocalDateTime.now());
        this.fillStrategy(metaObject,"createUser", BaseContext_id获取.getCurrentId());
        this.fillStrategy(metaObject,"updateUser", BaseContext_id获取.getCurrentId());


        //这种设置要求字段全有
//        metaObject.setValue("createTime", LocalDateTime.now());
//        metaObject.setValue("updateTime",LocalDateTime.now());
//        metaObject.setValue("createUser", BaseContext_id获取.getCurrentId());
//        metaObject.setValue("updateUser", BaseContext_id获取.getCurrentId());
    }

    /**
     * 更新操作，自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("公共字段自动填充[update]...");
        log.info(metaObject.toString());

        this.fillStrategy(metaObject,"updateTime",LocalDateTime.now());
        this.fillStrategy(metaObject,"updateUser", BaseContext_id获取.getCurrentId());


//        metaObject.setValue("updateTime",LocalDateTime.now());
//        metaObject.setValue("updateUser", BaseContext_id获取.getCurrentId());
    }
}